home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / libs / knowhow4 / fsys.cpp < prev    next >
C/C++ Source or Header  |  1994-10-10  |  5KB  |  197 lines

  1. #include "fsys.h"
  2. #include <dos.h>
  3. #include <search.h>
  4.  
  5. #define PATH_LEN 80
  6.  
  7. int disk(char* drive)
  8.     {
  9.     return strlwr(drive)[0] - 'a';
  10.     }
  11. ///////////////////////////////////
  12. int change_directory(int num) // looks, if it is possible to go to this directory
  13.                // if ".." - one level up, if "any name" - down
  14.     {
  15.     if(!chdir(global[num]))   // if we can go there
  16.     return 1;
  17.     return 0;
  18.     }
  19. //////////////////
  20. int sort_function( const void *a, const void *b)
  21.     {
  22.     return( strcmp(*(char**)a, *(char**)b) );
  23.     }
  24. ///////////////////
  25. void get_dir(void)
  26.     {
  27.     struct ffblk ffblk;
  28.     int done;
  29.     global_num = 1;
  30.     if(!*global[0])  // nothing from edit line
  31.     {
  32.     delete global[0];
  33.     global[0] = strdup("*.*");
  34.     }
  35.  
  36.     char drive[MAXDRIVE]; char dir[MAXDIR]; char file[MAXFILE];
  37.     char ext[MAXEXT];
  38.     int flags;
  39.     flags = fnsplit(global[0], drive, dir, file, ext);
  40.  
  41.     if(flags & DRIVE)
  42.     setdisk(disk(drive));
  43.     if(flags & DIRECTORY)
  44.     chdir(dir);
  45.  
  46.     delete global[0];
  47.     global[0] = strdup(strcat(file, ext));
  48.  
  49.     char* mask = strdup(global[0]);  // mask for file list creation
  50.  
  51.     done = findfirst(mask, &ffblk, 0);
  52.     int i = 0;
  53.     while(!done)
  54.     {
  55.     if(global_num > MAX_GLOBAL - 2)
  56.         break;                                      // global_num == 200
  57.     delete global[global_num];            // if file with given mask
  58.     global[global_num] = strdup(strlwr(ffblk.ff_name));
  59.     done = findnext(&ffblk);
  60.     global_num++;
  61.     i++;
  62.     }
  63.     qsort((void*)(global + 1), i, sizeof(char*), sort_function);
  64.  
  65.  
  66.     done = findfirst("*.*", &ffblk, FA_DIREC);
  67.  
  68.     int j = i;
  69.     i = 1;
  70.     while(!done)
  71.     {
  72.     if(global_num > MAX_GLOBAL - 2)
  73.         break;                                      // global_num == 200
  74.     if(ffblk.ff_attrib & FA_DIREC && strcmp(".", ffblk.ff_name))
  75.         {                      // if directory
  76.         delete global[global_num];
  77.         global[global_num] = strdup(ffblk.ff_name);
  78.         global_num++;
  79.         i++;
  80.         }
  81.     done = findnext(&ffblk);
  82.     }
  83.     if(i > 1)
  84.     qsort((void*)(global + j + 1), i - 1, sizeof(char*), sort_function);
  85.     delete global[global_num];
  86.     global[global_num] = strdup("");
  87.     delete mask;
  88.     }
  89. //////////////////////////////
  90. void new_file()  // return correct file name
  91.     {
  92.     char* a;
  93.     delete global[1];    delete global[2];    global[2] = strdup("");
  94.     if(a = strstr(global[0], ".*"))
  95.     {
  96.     global[1] = strdup("work");
  97.     return;
  98.     }
  99.     if(a = strstr(global[0], "*."))
  100.     {
  101.     global[1] = strdup("work");
  102.     strcat(global[1], a + 1);
  103.     return;
  104.     }
  105.     global[1] = strdup(global[0]);
  106.     }
  107. ///////////////////////////////
  108. FileSystem::FileSystem(rect coords)
  109.     : TextMenu(coords, "", "",
  110.            /* HOT */ NULL, /* POS */ 1, /* START */ 1,
  111.            /* ITEMSTRINGS */ NULL,
  112.            /* STATUSPOS */ rect(0, 24, 79, 25),
  113.            /* STATUSTYPE */ 0, /* STATUSSTRINGS */ NULL,
  114.            /* STATUSLIST */ NULL,
  115.            /* res */ FIXED, /* s */ 0, /* b_type */ SHOW_BORDER,
  116.            /* hdr_b_type */ NO_BORDER, 16, /* hdr_pat */ 0)
  117.     {
  118.     reserv = new char[MAXPATH + 10];
  119.     }
  120. /////////////////////////
  121. void FileSystem::show()
  122.     {
  123.     redraw = 2;
  124.     TextMenu::show();
  125.     }
  126. //////////////////////////
  127. void FileSystem::exe(int )
  128.     {
  129.     FILE* tmp_file = fopen(global[0], "a");
  130.  
  131.     if(!fclose(tmp_file))                     // If file name is legal.
  132.     {
  133.     global_i[0] = AC_OK;
  134.     global_i[1] = 1;
  135.     char work[MAXPATH];
  136.     getcwd(work, MAXPATH);
  137.     strcat(work, "\\");
  138.     strcat(work, global[0]);
  139.     delete global[1];
  140.     global[1] = strdup(work);
  141.     e.what = KEYEVENT;
  142.         strcpy(reserv, global[0]);
  143.     return;
  144.     }
  145.     while(1)
  146.     {
  147.     if(global_i[0] == AC_SELECT
  148.         || (redraw == 2 &&
  149.             e.what == MOUSEEVENT && user_screen().contains(e.where())))
  150.         {
  151.             global_i[0] = AC_SELECT;
  152.             if((strcmp(global[0], reserv) && *global[0] != '\0')
  153.                 || redraw)
  154.                 {
  155.                 mouseHideCursor();
  156.                 get_dir();                   // Read the list of files.
  157.                 if(*global[1] == '\0')       // Clear disk.
  158.             new_file();
  159.                 setItems();                    // assign file list to item list
  160.                 start = pos = 1;
  161.                 calcConsts();
  162.  
  163.                 TextMenu::show();
  164.                 mouseShowCursor();
  165.         moveTo(1);                     // and show them
  166.                 }
  167.             redraw = 0;
  168.         }
  169.         strcpy(reserv, global[0]);
  170.     TextMenu::exe((global_i[0] == AC_SELECT
  171.                || global_i[0] == AC_NULL) ? 0 : global_i[0]);
  172.     global_i[1] = global_num;       // global_i[1] reserved for file system
  173.  
  174.     if((global_i[0] == AC_SELECT && !change_directory(global_num)))  // it is not directory and RETURN pressed
  175.         {
  176.         e.what = KEYEVENT;
  177.         if(e.key != EVENT_TAB)
  178.         e.key = EVENT_RETURN;
  179.         char work[MAXPATH];
  180.         getcwd(work, MAXPATH);
  181.         strcat(work, "\\");
  182.         strcat(work, global[global_i[1]]);
  183.         delete global[global_i[1]];
  184.         global[global_i[1]] = strdup(work);
  185.         global_num = 1;
  186.         global_i[0] = 0;
  187.         return;
  188.         }
  189.         else
  190.             redraw = 1;
  191.     if(global_i[0] != AC_SELECT)
  192.         return;
  193.     }
  194.  
  195.     }
  196. ////////////////////////
  197.